package com.goods.controller.business;

import com.goods.business.service.HealthService;
import com.goods.common.model.system.User;
import com.goods.common.response.ResponseBean;
import com.goods.common.utils.JWTUtils;
import com.goods.common.vo.business.HealthVO;
import com.goods.common.vo.system.PageVO;
import com.goods.system.mapper.UserMapper;
import org.apache.catalina.valves.HealthCheckValve;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RequestMapping("/business/health")
@RestController
public class HealthController {

    @Autowired
    private HealthService healthService;
    @Resource
    private UserMapper userMapper;

    //当前日期打卡信息查询
    @GetMapping("/isReport")
    public ResponseBean isReport(HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String username = JWTUtils.getUsername(token);
        Example example = new Example(User.class);
        example.createCriteria().andEqualTo("username",username);
        User user = userMapper.selectOneByExample(example);
        Long id = user.getId();
        boolean flag = healthService.isReport(id);
        return ResponseBean.success(flag);
    }
    //立即打卡
    @PostMapping("/report")
    public ResponseBean report(@RequestBody HealthVO healthVO, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String username = JWTUtils.getUsername(token);
        Example example = new Example(User.class);
        example.createCriteria().andEqualTo("username",username);
        User user = userMapper.selectOneByExample(example);
        Long id = user.getId();
        healthService.report(healthVO,id);
        return ResponseBean.success();
    }
    //打卡历史记录
    @GetMapping("/history")
    public ResponseBean history(@RequestParam Map<String,Object> queryMap,HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String username = JWTUtils.getUsername(token);
        Example example = new Example(User.class);
        example.createCriteria().andEqualTo("username",username);
        User user = userMapper.selectOneByExample(example);
        Long id = user.getId();
        PageVO<HealthVO> history = healthService.history(queryMap, id);
        return ResponseBean.success(history);
    }
}
